
dataObj = { 
			sampleHot : null,
			dataSet : [],		// 数据源
			dataArr : [],		// 
			oldSet  : [],		// 之前已被选择的数据
			searchSource : [],   // 初始搜索数据源
			titleArr: [],		// 头部每列的标题
			tableMap : new Map(),
			currBoxId : null,
			currSelectSample : new Map()
	}

var HandDataTable = function(){
	 this.init = false;
	 this.tb_render_fun = {
				renderCheckBox : function(){ // instance, td, row, col, prop, value, cellProperties 
					var param = arguments;
					var instance = arguments[0][0];
					var row = param[0][2], td = param[0][1];
					var data = instance.getDataAtRow(row) ;
					var key = data[1];
					var select = false;
					if( dataObj.currSelectSample.containsKey( key )){
						select = true;
					}
					var html = '<input type="checkbox" '+ (select ? ' checked="checked"' : '')+' class="test"  data-id="'+data[1]+'" data-index="'+row+'">';
					$(td).css({"text-align":"left","vertical-align": "inherit"})
					var  td = param[0][1];
					td.innerHTML = html;
					return td;
				},
				renderOtherField : function() {
					  var param = arguments, row = param[0][2], td = param[0][1],value = param[0][5];
					  value = value ? value:"";
				      var tmpdata = '<div title="'+value+'"><span>'+value+'</span></div>';
				      td.innerHTML = tmpdata;
					  return td;
				},
				renderIdField : function() {
					  var param = arguments, row = param[0][2], td = param[0][1],value = param[0][5];
					 //
					  if(window.url.indexOf("goId")>0){
						  val = value?('<a href="'+window.url.replace("{goId}",value)+'" target="_blank">'+value+'</a>'):"";
						  
					  }else if(window.url.indexOf("pathwayId")>0){
						  val = value?('<a href="'+window.url.replace("{pathwayId}",value)+'" target="_blank">'+value+'</a>'):"";
					  }
					  
				      td.innerHTML = val;
				      
					  return td;
				},
				// 初始化表格头部标题以及列对应的数据字段
				initDataArrAndTitle : function( data ){
					var _this = this;
					dataObj.dataSet = data.data;
					 window.url=data.url;
					var tmp = data.title;
					dataObj.titleArr = [];
					dataObj.dataArr = [];
					$.each( tmp , function(i,n){
						dataObj.titleArr.push( "<span style='font-weight:bold;text-align:left' title='"+data.tipsArr[i]+"'>"+n+"</span>" );
						
					
						if(n=="Go ID"|| n=="Pathway ID"){
							
							if(data.url!=""){
								dataObj.dataArr.push({type: 'text',data:n,readOnly:true,renderer:function(){_this.renderIdField(arguments);} })
							}
							
						}else if(n=="Genesymbol"){
							dataObj.dataArr.push({type: 'text',data:n,readOnly:false,renderer: "html" })
						}else{
							dataObj.dataArr.push({type: 'text',data:n,readOnly:true,renderer: "html" })
						}
					})
					
					
				
				},
				
				renderTable : function ( containner , flag){
					var a = [];
					$.map(dataObj.dataArr,function(n){
						a.push(250);
					})
					var showfilter = false;
					if( $(containner).hasClass("datafilter")){
						showfilter = true;
					}
					$(containner).unbind();
					$("#loadingTableData").hide();
					console.log( dataObj.dataSet)
					dataObj.sampleHot = new Handsontable(containner, {
							  data: dataObj.dataSet,
							  rowHeaders: true,//行头，默认为1,2,3...,也可以自定义
							  colHeaders: true,//行头，默认为A,B,C...,也可以自定义
							  colWidths: 180,
							  stretchH: 'all',//自动拉伸到相应表格宽度，last：拉伸最后一列，all：全部拉伸，none：不拉伸
							  manualColumnResize: true,//列可用鼠标拖动宽度
							  manualRowResize: true,//行可用鼠标拖动宽度
							  fixedRowsTop: 0,//固定上方两行数据
							  fixedColumnsLeft: 1,//固定左边两列数据
							  minSpareRows: 0,//总是留一行空白可输入行
							  contextMenu: false,//左键快捷键
							  columnSorting: false,//列排序，点击某一列，按某列排序
							  viewportRowRenderingOffset:3,
							  viewportColumnRenderingOffset:dataObj.titleArr.length,
							  colHeaders: dataObj.titleArr,//自定义列头
							  columns: dataObj.dataArr,//数据插入表格
					    	  className: "htLeft",
							  persistentState: false,
							  disableVisualSelection:true,
							  renderAllRows: false,
							  dropdownMenu: showfilter,
							  filters : showfilter,
								});
   					this.bindEvent( containner ,flag );
				},
				
				bindEvent : function( containner , flag){
					Handsontable.Dom.addEvent(containner, 'mouseup', function (event) {
						if (event.target.nodeName == 'INPUT' 
							&& event.target.className == 'x-selectall') {
							  isChecked = !event.target.checked;
							  for(var i=0;i<dataObj.dataSet.length;i++){
								  dataObj.dataSet[i].isActive = isChecked
							  }
							  dataObj.sampleHot.render();
						}
						if (event.target.nodeName == 'INPUT' 
							&& event.target.className == 'test') {
							var index = $(event.target).data("index");
							var id = $(event.target).data("id");
							var checked= !event.target.checked;
							dataObj.dataSet[index].isActive = checked;
							if( checked ){
								dataObj.currSelectSample.put(id,dataObj.tableMap.get(id));
								dataObj.tableMap.get(id).isActive = true;
							} else {
								dataObj.currSelectSample.remove(id);
								dataObj.tableMap.get(id).isActive = false;
							}
							if(dataObj.currSelectSample.size() > 0 ){
								$(".edit_group_name").show();
							} else {
								$(".edit_group_name").hide();
							}
								
						}
					  });
				}
				
				
		};
		// TODO  hhh
	/** //隐藏
	 HandsontablehideRow: function(){
		获取总的行数
		var plugin = dataObj.sampleHot.getPlugin('hiddenRows');
		var  tableRowstotals= dataObj.sampleHot.countRows();
		if(tableRowstotals>=10){
			for(;tableRowstotals--;tableRowstotals<=9){
				plugin.hideRow(tableRowstotals);
			}
		}
		
	 };
	//显示
	HandsontableshowRow: function(){
		var plugin = dataObj.sampleHot.getPlugin('hiddenRows');
		var  tableRowstotals= dataObj.sampleHot.countRows();
		if(tableRowstotals>=10){
			for(;tableRowstotals--;tableRowstotals<=9){
				plugin.showRow(tableRowstotals);
			}
		}
	};
	 */
	 
	 this.showTable = function( containner , data ,flag){
		 	 var _self = this;
			 $(containner).empty("")
			 var startDate = new Date();
			 this.tb_render_fun.initDataArrAndTitle( data ) ;
			 this.tb_render_fun.renderTable( containner, flag ) ; 
			 var endDate = new Date();
			 console.log(startDate - endDate)
			 dataObj.sampleHot.render();
		 
	 },
	 
	 this.updateSetting = function ( dataSet ){
		 if( dataSet ){
			 this.tb_render_fun.handleData( dataSet );
			 dataObj.sampleHot.updateSettings({ data: dataObj.dataSet })
		 } else {
			 dataObj.sampleHot.updateSettings({ data: dataObj.searchSource })
		 }
		 
	 } 
}
